{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6844ef40-e63b-4366-bb5e-4dd4e681e281",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "from PIL import Image\n",
    "names = os.listdir('olivettifaces')\n",
    "\n",
    "directory = 'olivettifaces'\n",
    "names = os.listdir(directory)\n",
    "\n",
    "img0 = Image.open('olivettifaces\\\\' + names[0])\n",
    "img0 = img0.convert('L')\n",
    "\n",
    "img0 = img0.resize((32, 32))\n",
    "arr = np.array(img0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e540d8a2-cd57-4555-9e56-f237907031e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32, 32)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e6c6af55-fead-48ea-ba5f-23bbcb64f8dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = arr.reshape(1, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fada1c67-4ee1-4231-ac47-25edf80a26f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 1024)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "db242dcd-f176-4a24-bb3f-185735d94395",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = []\n",
    "for i in names:\n",
    "    img = Image.open('olivettifaces\\\\' + i)\n",
    "    img = img.convert('L')\n",
    "    img = img.resize((32, 32))\n",
    "    arr = np.array(img)\n",
    "    X.append(arr.reshape(1, -1).flatten().tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0c2a879c-f2a3-4656-97d5-fde8cc95a9d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2250dddf-d81c-41d5-8b9d-08afe092c677",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = pd.DataFrame(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7f516ee0-a761-47b6-a78a-24cc43106080",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(400, 1024)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "98d76855-c2b9-46ea-81ca-99fb3b7f75e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = []\n",
    "for i in names:\n",
    "    img = Image.open('olivettifaces\\\\' + i)\n",
    "    y.append(int(i.split('_')[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6b7cccfd-3c2b-427e-b5fc-96f01cf7f94f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.decomposition import PCA\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)\n",
    "pca = PCA(n_components=100)\n",
    "pca.fit(X_train)\n",
    "\n",
    "X_train_pca = pca.transform(X_train)\n",
    "X_test_pca = pca.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "281d9604-98a7-42c9-b145-32b812ec2b3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(320, 100)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train_pca.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "076a5d02-5478-4c1b-829a-e71cbbfe72e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(80, 100)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test_pca.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "252758f3-0a33-40ca-92b9-b9e8de14d4eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn = KNeighborsClassifier()\n",
    "knn.fit(X_train_pca, y_train)\n",
    "y_pred = knn.predict(X_test_pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9aa8d2a0-d93a-488a-86f1-18527b02daa1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9125\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "score = accuracy_score(y_pred, y_test)\n",
    "print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "05f8408c-508e-4eaf-b144-da0f0c2f477f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9125\n"
     ]
    }
   ],
   "source": [
    "#使用原始数据 不降维的\n",
    "knn = KNeighborsClassifier()\n",
    "knn.fit(X_train, y_train)\n",
    "y_pred = knn.predict(X_test)\n",
    "\n",
    "from sklearn.metrics import accuracy_score\n",
    "score = accuracy_score(y_pred, y_test)\n",
    "print(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "775a8562-f8df-441b-9cd5-104680747d3d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
